package cn.lg.soar.system.biz.modules.auth.model;

import cn.lg.soar.common.util.data.DataUtil;
import cn.lg.soar.common.util.trans.annotations.TranslateName;
import cn.lg.soar.core.annotations.constraints.group.Create;
import cn.lg.soar.core.annotations.constraints.group.Update;
import cn.lg.soar.database.translate.TranslateByEntity;
import cn.lg.soar.system.biz.modules.auth.entity.Component;
import cn.lg.soar.system.biz.modules.auth.entity.Permit;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author luguoxiang 469599794@qq.com
 * @Date: 2025/5/3 18:07
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Getter
@Setter
public class ComponentVO {


    /**  */
    @NotNull(message = "主键不能为空", groups = { Update.class })
    @ExcelIgnore
    @Schema(description = "主键")
    @TableId(type = IdType.AUTO)
    private Integer id;

    /** 组件名称 */
    @Schema(description = "组件名称")
    private String name;

    /** 权限码 */
    @Schema(description = "权限码")
    private String code;

    /** 组件路径 */
    @Schema(description = "组件路径")
    private String path;

    /** 是否共享的 */
    @Schema(description = "是否共享的")
    private Boolean shared;

    /** 是否缓存的 */
    @Schema(description = "是否缓存的")
    private Boolean keepalive;

    /** 接口权限ids */
    @TranslateName("permits")
    @TranslateByEntity(value = Permit.class)
    @Schema(description = "接口权限ids")
    private Integer[] permitIds;

    @Schema(description = "页面元素")
    private List<Element> elements;

    public ComponentVO() {
    }

    public ComponentVO(Component component, List<Component> elements) {
        this.id = component.getId();
        this.name = component.getName();
        this.code = component.getCode();
        this.path = component.getPath();
        this.shared = component.getShared();
        this.keepalive = component.getKeepalive();
        this.permitIds = component.getPermitIds();
        if (DataUtil.isValuable(elements)) {
            this.elements = elements.stream().map(x -> {
                Element element = new Element();
                element.code = x.getCode();
                element.name = x.getName();
                element.permitIds = x.getPermitIds();
                return element;
            }).collect(Collectors.toList());
        }
    }

    @Getter
    @Setter
    public static class Element {
        /** 组件名称 */
        @NotBlank(message = "组件名称不能为空", groups = { Create.class, Update.class })
        @Schema(description = "组件名称")
        private String name;

        /** 权限码 */
        @NotBlank(message = "权限码不能为空", groups = { Create.class, Update.class })
        @Schema(description = "权限码")
        private String code;

        /** 接口权限ids */
        @Schema(description = "接口权限ids")
        private Integer[] permitIds;
    }
}
